package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.sc.nft.dao.EcologyHatchRatioDao;
import com.sc.nft.entity.EcologyHatchRatio;
import com.sc.nft.entity.vo.EcologyHatchMetaMapVO;
import com.sc.nft.enums.metamap.HatchRatioTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.EcologyHatchRatioService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 熔炼权益配置服务接口实现
 *
 * @author jinpinhai
 * @since 2024-09-06 16:29:30
 * @description Generator
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchRatioServiceImpl implements EcologyHatchRatioService {
    private final EcologyHatchRatioDao ecologyHatchRatioDao;

    @Override
    public EcologyHatchMetaMapVO findMetaMapRatio() {
        List<EcologyHatchRatio> metaMapRatio = ecologyHatchRatioDao.findMetaMapRatio(Lists.newArrayList(HatchRatioTypeEnum.PROVINCE, HatchRatioTypeEnum.CITY, HatchRatioTypeEnum.REGION));
        if (CollectionUtils.isEmpty(metaMapRatio)) {
            EcologyHatchMetaMapVO ecologyHatchMetaMapVO = new EcologyHatchMetaMapVO();
            ecologyHatchMetaMapVO.setMetaMapProvinceRatio(BigDecimal.ZERO);
            ecologyHatchMetaMapVO.setMetaMapCityRatio(BigDecimal.ZERO);
            ecologyHatchMetaMapVO.setMetaMapRegionRatio(BigDecimal.ZERO);
            return ecologyHatchMetaMapVO;
        }
        return getEcologyHatchMetaMapVO(metaMapRatio);
    }


    @Override
    public void saveMetaMapRatio(BigDecimal metaMapRatio, BigDecimal metaMapRatioCity, BigDecimal metaMapRatioRegion) {
        for (HatchRatioTypeEnum value : HatchRatioTypeEnum.values()) {
            List<EcologyHatchRatio> metaMapRatioList = ecologyHatchRatioDao.findMetaMapRatio(Lists.newArrayList(value));
            EcologyHatchRatio save = new EcologyHatchRatio();
            if (value == HatchRatioTypeEnum.PROVINCE) {
                save.setShareRatio(metaMapRatio);
            }
            if (value == HatchRatioTypeEnum.CITY) {
                save.setShareRatio(metaMapRatioCity);
            }
            if (value == HatchRatioTypeEnum.REGION) {
                save.setShareRatio(metaMapRatioRegion);
            }
            if (CollectionUtils.isEmpty(metaMapRatioList)) {
                save.setRatioType(value);
                if (!ecologyHatchRatioDao.save(save)) {
                    throw new GlobalRunTimeException("版图比例保存失败");
                }
            } else {
                EcologyHatchRatio ecologyHatchRatio = metaMapRatioList.get(0);
                if (!ecologyHatchRatioDao.updateMetaMapRatio(ecologyHatchRatio.getId(), save.getShareRatio())) {
                    throw new GlobalRunTimeException("版图比例更新失败");
                }
            }
        }
    }


    private static EcologyHatchMetaMapVO getEcologyHatchMetaMapVO(List<EcologyHatchRatio> metaMapRatio) {
        EcologyHatchMetaMapVO ecologyHatchMetaMapVO = new EcologyHatchMetaMapVO();
        for (EcologyHatchRatio ecologyHatchRatio : metaMapRatio) {
            if (ecologyHatchRatio.getRatioType() == HatchRatioTypeEnum.PROVINCE) {
                ecologyHatchMetaMapVO.setMetaMapProvinceRatio(ecologyHatchRatio.getShareRatio());
            }
            if (ecologyHatchRatio.getRatioType() == HatchRatioTypeEnum.CITY) {
                ecologyHatchMetaMapVO.setMetaMapCityRatio(ecologyHatchRatio.getShareRatio());
            }
            if (ecologyHatchRatio.getRatioType() == HatchRatioTypeEnum.REGION) {
                ecologyHatchMetaMapVO.setMetaMapRegionRatio(ecologyHatchRatio.getShareRatio());
            }

        }
        return ecologyHatchMetaMapVO;
    }

}